//
//  WelcomeController.m
//  MYChatting
//
//  Created by mac on 16/8/16.
//  Copyright © 2016年 lhf. All rights reserved.
//

#import "WelcomeController.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"
#import "UserInfo.h"
#import "MYTabBarController.h"

@interface WelcomeController ()

@property (nonatomic,weak) UIImageView *backView;

@property (nonatomic,weak) UIImageView *iconView;
@property (nonatomic,weak) UILabel *welcomeLabel;

@end

@implementation WelcomeController


- (void)loadView{
    UIImageView *backView = [[UIImageView alloc] init];
    backView.image = [UIImage imageNamed:@"ad_background"];
    self.backView = backView;
    self.view = backView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self setUpUI];
}


- (void)setUpUI{
    
    UserInfo *userInfo = [[UserInfo alloc] loadUserInfo];
    
    UIImageView *iconView = [[UIImageView alloc] init];
    [iconView sd_setImageWithURL:[NSURL URLWithString:userInfo.avatar_large] placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
    
    self.iconView = iconView;
    [self.view addSubview:self.iconView];
  
    
    UILabel *welcomeLabel = [[UILabel alloc] init];
    welcomeLabel.text = [NSString stringWithFormat:@"欢迎%@回来",userInfo.name];
    welcomeLabel.textColor = [UIColor grayColor];
    welcomeLabel.font = [UIFont systemFontOfSize:16];
      self.welcomeLabel = welcomeLabel;
    [self.view addSubview:self.welcomeLabel];
  
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).with.offset(-220);
        make.size.mas_equalTo(CGSizeMake(90, 90));
    }];
    
    [self.welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.iconView.mas_centerX);
        make.top.equalTo(self.iconView.mas_bottom).with.offset(10);
        
    }];
    
    //设置圆角
    self.iconView.layer.cornerRadius = 45;
    self.iconView.layer.masksToBounds = YES;
}



//动画一般在viewDidAppear方法中实现
//- (void)viewDidAppear:(BOOL)animated{
//    [super viewDidAppear:animated];
//    
//    self.iconView.transform = CGAffineTransformMakeScale(0.65, 0.65);
//    [UIView animateWithDuration:0.5 animations:^{
//        self.iconView.transform = CGAffineTransformIdentity;
//    } completion:^(BOOL finished) {
//        
//        //[UIApplication sharedApplication].keyWindow.rootViewController = [[MYTabBarController alloc] init];
//    }];
//}


- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    CGFloat offset = - [UIScreen mainScreen].bounds.size.height + 150;
    self.welcomeLabel.alpha = 0;
    
    [UIView animateWithDuration:0.5 delay:0 usingSpringWithDamping:0.98 initialSpringVelocity:9.8 options:0
    animations:^{
        
        [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view.mas_bottom).offset(offset);
        }];
         [self.view layoutIfNeeded];
        
        self.welcomeLabel.alpha = 1;
        
    } completion:^(BOOL finished) {
        
        //发布通知
        [[NSNotificationCenter defaultCenter] postNotificationName:MYSwitchRootVCNotification object:nil];
        
    }];
}

@end
